/**
 * Copyright 2009 University of  Washington
 * This code is released under the GPL version 3 - with a copy of the license included
 * @author Maya Hao Li
 *
 */
package seedpod.model.rdb;

import seedpod.util.XMLWriter;

public class ViewReference extends Attribute
{
	private Attribute _toAtt; // attribute that's referenced in the view
	private View _toView;

	public ViewReference(Rdb schema, Relation fromTable, String fromAtt, View refView, String toAtt, RdbValueType type)
			throws NoRelationFoundException {
		super( schema, fromTable, fromAtt, type );
		_toView = refView;
		setReferencedAttribute( refView );
	}

	public Attribute getReferencedAttribute() {
		return _toAtt;
	}

	public void setReferencedAttribute( View view ) {
		Attribute toPK = view.getPrimaryKey();
		if ( toPK != null )
			_toAtt = toPK;
	}
	
	public View getReferencedView() {
		return _toView;
	}

	public XMLWriter serializeXML( XMLWriter out ) throws XMLWriter.XMLException {
		XMLWriter fk;
		fk = out.addElement( "viewKey" );
		fk.addElement( "vkName", getName() );
		fk.addElement( "referenceTable", getContainerRelationName() );
		fk.addElement( "referencedView", _toView.getName() );
		fk.addElement( "referencedColumn", _toAtt.getName() );
		return out;
	}
}
